package org.fing.edu.uy.esb.adapative.ws.testing;

import java.util.Scanner;

import org.fing.edu.uy.esb.adaptative.ws.egov.DelayStore;
import org.fing.edu.uy.esb.adaptative.ws.egov.bps.BPSWS;
import org.fing.edu.uy.esb.adaptative.ws.egov.dgi.DGIWS;
import org.fing.edu.uy.esb.adaptative.ws.egov.dnic.DNICWS;



public class Main {
	

	private static final int BPS_WS_PORT = 54330;
	private static final int DGI_WS_PORT = 54331;
	private static final int DNIC_WS_PORT = 54332;
	
	private static final String BPS_WS_NAME = "BPSWS";
	private static final String DGI_WS_NAME = "DGIWS";
	private static final String DNIC_WS_NAME = "DNICWS";
	
	private static DGIWS dgiws = null;
	private static BPSWS bpsws = null;
	private static DNICWS dnicws = null;
	 
	public static void main (String [] args){
		
		 System.out.println("**************************************************************************");
		 System.out.println("*********   SERVICIOS TEST PARA ESB-ADAPTATIVO (MODIFICADOS)   ***********");
		 System.out.println("**************************************************************************");
		 
		 printMenu();
		 
		 int option;
		 Scanner scanIn = new Scanner(System.in);
		 
		 while(true){
			 
			 try{
				 option = Integer.parseInt(scanIn.nextLine());
				 switch (option) {			
				    case 1:
				    	flipServices();
						break;
					case 2:
						setDelayBPS();
						break;
					case 3:
						setDelayDGI();
						break;	
					case 4:
						setDelayDNIC();
						break;		
					case 5:						
						System.exit(1);
						break;	
					case 6:
						System.out.println("Bye bye.....");
						System.exit(3);
						break;							
					default:
						break;
				}
				 
			 }catch (Exception ex) {
				 System.out.println("Ingrese una opcion correcta -> ");
				 ex.printStackTrace();
			 }
			 
			 printMenu();
			
		 }
	}
	
	public static void flipServices(){
		if(bpsws!=null && bpsws.getEndpoint().isPublished()){
			downServices();
		}else{
			initServices();
		}
	}
	

	private static void setDelayBPS() {
		 long option;
		 Scanner scanIn = new Scanner(System.in);
		 
		 System.out.println("Inserte valores en minisegundos");
		 System.out.println("Max delay para los servicios del BPS");
		 option = Long.parseLong(scanIn.nextLine());
		 DelayStore.getInstance().setBpsDelayMax(option);
		 
		 System.out.println("Min delay para los servicios del BPS");
		 option = Long.parseLong(scanIn.nextLine());
		 DelayStore.getInstance().setBpsDelayMin(option);
		 
		 System.out.println("Definir x para x/10 fallos para el BPS");
		 option = Long.parseLong(scanIn.nextLine());
		 DelayStore.getInstance().setBpsFault(option);
		 
		 System.out.println("Maximo de invocaciones por minuto para el BPS");
		 option = Long.parseLong(scanIn.nextLine());
		 DelayStore.getInstance().setBpsMaxInvokexMin(option);
		 
	}	 	
	
	private static void setDelayDGI() {
		 long option;
		 Scanner scanIn = new Scanner(System.in);
		 
		 System.out.println("Max delay para los servicios del DGI");
		 option = Long.parseLong(scanIn.nextLine());
		 DelayStore.getInstance().setDgiDelayMax(option);
		 
		 System.out.println("Min delay para los servicios del DGI");
		 option = Long.parseLong(scanIn.nextLine());
		 DelayStore.getInstance().setDgiDelayMin(option);
		 
		 System.out.println("Definir x para x/10 fallos para el DGI");
		 option = Long.parseLong(scanIn.nextLine());
		 DelayStore.getInstance().setDgiFault(option);
		 
		 System.out.println("Maximo de invocaciones por minuto para el DGI");
		 option = Long.parseLong(scanIn.nextLine());
		 DelayStore.getInstance().setDgiMaxInvokexMin(option);		 
		 
	}
	
	private static void setDelayDNIC() {
		 long option;
		 Scanner scanIn = new Scanner(System.in);	
		 
		 System.out.println("Max delay para los servicios del DNIC");
		 option = Long.parseLong(scanIn.nextLine());
		 DelayStore.getInstance().setDnicDelayMax(option);
		 
		 System.out.println("Min delay para los servicios del DNIC");
		 option = Long.parseLong(scanIn.nextLine());
		 DelayStore.getInstance().setDnicDelayMin(option);
		 
		 System.out.println("Definir x para x/10 fallos para el DNIC");
		 option = Long.parseLong(scanIn.nextLine());
		 DelayStore.getInstance().setDnicFault(option);
		 
		 System.out.println("Maximo de invocaciones por minuto para el DNIC");
		 option = Long.parseLong(scanIn.nextLine());
		 DelayStore.getInstance().setDnicMaxInvokexMin(option);		
	}

	private static void printMenu() {
		 System.out.println("Eliga una opcion:");		 	
		 System.out.println("  1-"+ getServiceOption() + " servicios modificados");
		 System.out.println("  2-Configurar parametros BPS");
		 System.out.println("  3-Configurar parametros DGI");
		 System.out.println("  4-Configurar parametros DNIC");
		 System.out.println("  5-Cambiar al modo por defecto");
		 System.out.println("  6-Salir");
	}
		
	
	private static void initServices(){
		bpsws = new BPSWS();
		bpsws.publish(BPS_WS_PORT, BPS_WS_NAME);
		
		dgiws = new DGIWS();
		dgiws.publish(DGI_WS_PORT, DGI_WS_NAME);
		
		dnicws = new DNICWS();
		dnicws.publish(DNIC_WS_PORT, DNIC_WS_NAME);
	}

	
	
	private static void downServices(){		
		if(bpsws!=null){
			bpsws.unpublish();
		}
		
		if(dgiws!=null)	{
			dgiws.unpublish();
		}
		
		if(dnicws!=null){
			dnicws.unpublish();
		}
	}
	
		
	private static String getServiceOption(){
		if(bpsws!=null && bpsws.getEndpoint().isPublished()){
			return "Bajar";
		}else{
			return "Iniciar";
		}		
	}
	
	
}
